package com.hdaccp.hadoop.t174mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.util.Map;
public class WordCount1  implements Tool {

    private Configuration configuration ;


    public static class MyWorldCount extends Mapper<LongWritable,Text,Text,LongWritable>
    {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString() ;
            String[] lines = line.split(" ") ;
            for(String s:lines){
                context.write(new Text(s),new LongWritable(1));
            }
        }
    }

    public static class MyWorldCountReduce extends Reducer<Text,LongWritable,Text,LongWritable>
    {
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            long sum = 0 ;
            for(LongWritable num :values){
                sum = sum + num.get() ;
            }

            context.write(key,new LongWritable(sum));
        }
    }

    @Override
    public int run(String[] args) throws Exception {
        //默认要读取一个配置文件 core-site.xml
        Configuration configuration = this.getConf() ;
        //声明一个作业
        Job job = Job.getInstance(configuration,"mywordcount") ;
        //配置作业
        job.setJarByClass(WordCount.class);
        //1.配置mapper
        FileInputFormat.setInputPaths(job,new Path("/a1.txt"));
        job.setMapperClass(MyWorldCount.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        //2.配置reducer
        Path pathout = new Path("/out2") ;
        FileSystem fileSystem = FileSystem.get(configuration) ;
        if(fileSystem.exists(pathout)){
            fileSystem.delete(pathout,true) ;
        }
        FileOutputFormat.setOutputPath(job,pathout);
        job.setReducerClass(MyWorldCountReduce.class);
        return job.waitForCompletion(true) ? 0:1;
    }


    public static void main(String[] args) throws Exception{
        int exitCode = ToolRunner.run(new Configuration(),new WordCount1(), args);
        System.exit(exitCode);
    }

    @Override
    public void setConf(Configuration conf) {
        this.configuration = new Configuration() ;
    }

    @Override
    public Configuration getConf() {
        return this.configuration;
    }
}
